#include "max31865.h"
#include "delay.h"
#include "math.h"

unsigned char SPI_ReadWriteByte(unsigned char byte)
{
    uint8_t i;
    uint8_t Temp = 0x00;
    unsigned char SDI;

    for (i = 0; i < 8; i++)
    {
        MAX31865_SCK_LOW; // SCLK = 0; ???????
        if (byte & 0x80)
        {
            MAX31865_MOSI_HIGH; // SO=1
        }
        else
        {
            MAX31865_MOSI_LOW; // SO=0
        }
        byte <<= 1;

        MAX31865_SCK_HIGH;        // SCLK = 1;//??????????

        SDI = READ_MAX31865_MISO; //?��?si?????????
        Temp <<= 1;
        if (SDI)                  //????1?
        {
            Temp++;               //??1  ??????????��????1   ???????<<=1
        }
    }
    return Temp; //???????miso??????
}

//?????max31865 ??IO??
void MAX31865_Init(void)
{                                                          //?????SPI1
    GPIO_setAsOutputPin(GPIO_PORT_P3,GPIO_PIN3);  // SCK
    GPIO_setAsOutputPin(MAX31865_CS_PORT_PIN);   // CS
    GPIO_setAsOutputPin(GPIO_PORT_P6,GPIO_PIN0); // MOSI
    GPIO_setAsInputPin(GPIO_PORT_P3,GPIO_PIN2);  // MISO
    GPIO_setAsInputPin(MAX31865_RDY_PORT_PIN);   // 

    writeRegister8(0x00, 0x00);                            //??????��????
    enableBias(1);                                         //????????
    delay_ms(10);                                          //???10ms???RTDIN???????????
    setWires(MAX31865_3WIRE);                              //???PT100 ?????????
    clearFault();                                          //?????????��
}

// RTD??????????
void setWires(max31865_numwires_t wires)
{
    uint8_t t = readRegister8(MAX31856_CONFIG_REG);
    if (wires == MAX31865_3WIRE)
    {
        t |= MAX31856_CONFIG_3WIRE;
    }
    else
    {
        // 2 or 4 wire
        t &= ~MAX31856_CONFIG_3WIRE;
    }
    writeRegister8(MAX31856_CONFIG_REG, t);
}

//????????????
void autoConvert(unsigned char b)
{
    uint8_t t = readRegister8(MAX31856_CONFIG_REG);
    if (b)
    {
        t |= MAX31856_CONFIG_MODEAUTO; // enable autoconvert
    }
    else
    {
        t &= ~MAX31856_CONFIG_MODEAUTO; // disable autoconvert
    }
    writeRegister8(MAX31856_CONFIG_REG, t);
}

//????????
void enableBias(unsigned char b)
{
    uint8_t t = readRegister8(MAX31856_CONFIG_REG);
    if (b)
    {
        t |= MAX31856_CONFIG_BIAS; // enable bias
    }
    else
    {
        t &= ~MAX31856_CONFIG_BIAS; // disable bias
    }
    writeRegister8(MAX31856_CONFIG_REG, t);
}

//?????????8��
u8 readRegister8(u8 addr)
{
    uint8_t ret = 0;
    addr &= 0x7F; // make sure top bit is set to 1
    MAX31865_CS_LOW;
    delay_us(100);
    SPI_ReadWriteByte(addr);       //????��????????????
    ret = SPI_ReadWriteByte(0xff); // ��????????
    delay_us(100);
    MAX31865_CS_HIGH;              //?????
    return ret;
}

//?????????????16��
u16 readRegister16(u8 addr)
{
    uint8_t buffer[2] = {0, 0};
    uint16_t ret = 0;
    readRegisterN(addr, buffer, 2);

    ret = buffer[0];
    ret <<= 8;
    ret |= buffer[1];

    return ret;
}

//??N???????
void readRegisterN(u8 addr, u8 buffer[], u8 n)
{
    addr &= 0x7F; // make sure top bit is 0
    MAX31865_CS_LOW;
    delay_us(100);
    SPI_ReadWriteByte(addr); //?????????????????
    while (n--)
    {
        buffer[0] = SPI_ReadWriteByte(0Xff);
        buffer++;
    }
    delay_us(100);
    MAX31865_CS_HIGH; //?????
}

//????????��8��????
void writeRegister8(u8 addr, u8 dat)
{
    addr |= 0x80; // make sure top bit is set to 1
    MAX31865_CS_LOW;
    delay_us(100);
    SPI_ReadWriteByte(addr); //?????????????????
    SPI_ReadWriteByte(dat);  // ��????????
    delay_us(100);
    MAX31865_CS_HIGH;        //?????
}

u16 MAX31865_Get_ADC()
{
    uint8_t t = 0;
    uint16_t rtd = 0;
    t = readRegister8(MAX31856_CONFIG_REG);
    t |= MAX31856_CONFIG_1SHOT;
    writeRegister8(MAX31856_CONFIG_REG, t);
    delay_ms(70); //???????????????????????????50HZ????????????62ms??60hz?52ms
    rtd = readRegister16(MAX31856_RTDMSB_REG);
    // remove fault
    rtd >>= 1;

    return rtd;
}

void clearFault(void)
{
    uint8_t t = readRegister8(MAX31856_CONFIG_REG);
    t &= ~0x2C;
    t |= MAX31856_CONFIG_FAULTSTAT;
    writeRegister8(MAX31856_CONFIG_REG, t);
}


uint8_t MAX31865_SB_Read(uint8_t addr) // SPI Single-Byte Read
{
    uint8_t read = 0;
    uint8_t i;
    MAX31865_CS_LOW;
    delay_us(30);
    for (i = 0; i < 8; i++)
    {
        MAX31865_SCK_LOW;
        if (addr & 0x80)
        {
            MAX31865_MOSI_HIGH;
        }
        else
        {
            MAX31865_MOSI_LOW;
        }
        delay_us(30);
        MAX31865_SCK_HIGH;
        addr <<= 1;
        delay_us(30);
    }

    delay_us(30);
    for (i = 0; i < 8; i++)
    {
        MAX31865_SCK_LOW;
        read = read << 1;
        delay_us(30);
        if (READ_MAX31865_MISO)
        {
            read++;
        }
        MAX31865_SCK_HIGH;
        delay_us(30);
    }
    MAX31865_CS_HIGH;
    return read;
}

void MAX31865_SB_Write(uint8_t addr, uint8_t wdata) // SPI Single-Byte Write
{
    uint8_t i;
    MAX31865_CS_LOW;

    delay_us(30);
    for (i = 0; i < 8; i++)
    {
        MAX31865_SCK_LOW;
        if (addr & 0x80)
        {
            MAX31865_MOSI_HIGH;
        }
        else
        {
            MAX31865_MOSI_LOW;
        }
        delay_us(30);
        MAX31865_SCK_HIGH;
        addr <<= 1;
        delay_us(30);
    }

    for (i = 0; i < 8; i++)
    {
        MAX31865_SCK_LOW;
        if (wdata & 0x80)
        {
            MAX31865_MOSI_HIGH;
        }
        else
        {
            MAX31865_MOSI_LOW;
        }
        delay_us(30);
        MAX31865_SCK_HIGH;
        wdata <<= 1;
        delay_us(30);
    }
    MAX31865_CS_HIGH;
}
